﻿using System;
using System.Net;
using System.Xml;
using System.IO;
using System.Collections.Specialized;

using SBPweb.Common.Connectivity;

using SBPweb.Twitter.Host.Stanzas;

namespace SBPweb.Twitter.Host.Connectivity
{
    
    public class Client
    {

        #region Private / protected fields

        private Yedda.Twitter twitter = new Yedda.Twitter();
        private string userName;
        private string password;

        #endregion

        #region Constructor logic

        public Client()
        {
        }

        #endregion

        #region Methods

        public UserTimeLine SignIn(string userName, string password)
        {
            this.userName = userName;
            this.password = password;

            CookiedWebClient client = new CookiedWebClient();

            string path = string.Format("http://iwiw.hu/mobile/user/login.jsp?method=Login&email={0}&password={1}", userName, password);
            HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(path);

            hwr.Referer = "www.iwiw.net";
            hwr.Method = "GET";
            hwr.Credentials = CredentialCache.DefaultCredentials;
            hwr.Accept = "text/html";

            /*
            byte[] buffer = client.Encoding.GetBytes(string.Format("method=Login&email={0}&password={1}", userName, password));

            string path = "http://iwiw.hu/mobile/user/login.jsp";
            HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(path);
            hwr.CookieContainer = client.CookieContainer;
            hwr.ContentType = "application/x-www-form-urlencoded";
            hwr.ContentLength = buffer.Length;
            hwr.Referer = "www.iwiw.net";
            hwr.Method = "POST";
            hwr.Credentials = CredentialCache.DefaultCredentials;
            hwr.Accept = "text/html";
            Stream streamReq = hwr.GetRequestStream();
            streamReq.Write(buffer, 0, buffer.Length);
            streamReq.Close();
            */

            HttpWebResponse resp = (HttpWebResponse)hwr.GetResponse();
            Stream streamResp = resp.GetResponseStream();
            StreamReader reader = new StreamReader(streamResp);
            string s = reader.ReadToEnd();

            s = client.DownloadString("http://iwiw.hu/mobile/");
            //byte[] bytes = client.UploadValues(path, "GET", data);
            //string s = client.Encoding.GetString(bytes);

            //s = client.DownloadString("http://iwiw.hu/");

            XmlDocument ret = twitter.GetUserTimelineAsXML(userName, password);
            return new UserTimeLine(ret);
        }

        #endregion

    }
    
}